﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Sunny.Controls
{
	public class ControlList<T> : System.Collections.Generic.IList<T>,System.Collections.IList where T : System.Windows.UIElement
	{
		protected readonly System.Windows.Controls.UIElementCollection ControlCollection;

		public ControlList(System.Windows.Controls.UIElementCollection baseList)
		{
			this.ControlCollection = baseList;
		}

		#region IList<T> 成员

		public int IndexOf(T item)
		{
			return this.ControlCollection.IndexOf(item);
		}

		public void Insert(int index, T item)
		{
			this.ControlCollection.Insert(index, item);
		}

		public void RemoveAt(int index)
		{
			this.ControlCollection.RemoveAt(index);
		}

		public T this[int index]
		{
			get
			{
				return (T)this.ControlCollection[index];
			}
			set
			{
				this[index] = value;
			}
		}

		#endregion

		#region ICollection<T> 成员

		public void Add(T item)
		{
			this.ControlCollection.Add(item);
		}

		public void Clear()
		{
			this.ControlCollection.Clear();
		}

		public bool Contains(T item)
		{
			return this.ControlCollection.Contains(item);
		}

		public void CopyTo(T[] array, int arrayIndex)
		{
			this.ControlCollection.CopyTo(array, arrayIndex);
		}

		public int Count
		{
			get
			{
				return this.ControlCollection.Count;
			}
		}

		public bool IsReadOnly
		{
			get
			{
				return false;
			}
		}

		public bool Remove(T item)
		{
			this.ControlCollection.Remove(item);
			return true;
		}

		#endregion

		#region IEnumerable<T> 成员

		public IEnumerator<T> GetEnumerator()
		{
			return this.ControlCollection.OfType<T>().GetEnumerator();
		}

		#endregion

		#region IEnumerable 成员

		System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
		{
			return this.ControlCollection.GetEnumerator();
		}

		#endregion

		//#region IList 成员

		//public int Add(object value)
		//{
		//    this.ControlCollection.Add((System.Web.UI.Control)value);
		//    return this.Count-1;
		//}

		//public bool Contains(object value)
		//{
		//    return this.ControlCollection.Contains((System.Web.UI.Control)value);
		//}

		//public int IndexOf(object value)
		//{
		//    return this.ControlCollection.IndexOf((System.Web.UI.Control)value);
		//}

		//public void Insert(int index, object value)
		//{
		//    this.ControlCollection.Insert(index, (System.Web.UI.Control)value);
		//}

		//public bool IsFixedSize
		//{
		//    get
		//    {
		//        return false;
		//    }
		//}

		//public void Remove(object value)
		//{
		//    this.ControlCollection.Remove((System.Web.UI.Control)value);
		//}

		//object System.Collections.IList.this[int index]
		//{
		//    get
		//    {
		//        return this.ControlCollection[index];
		//    }
		//    set
		//    {
		//        this.ControlCollection.RemoveAt(index);
		//        this.ControlCollection.AddAt(index, (System.Web.UI.Control)value);
		//    }
		//}

		//#endregion

		#region ICollection 成员

		public void CopyTo(Array array, int index)
		{
			this.ControlCollection.CopyTo(array, index);
		}

		int IList.Add(object value)
		{
			this.Add((T)value);
			return this.Count - 1;
		}

		bool IList.Contains(object value)
		{
			return this.Contains((T)value);
		}

		void IList.Clear()
		{
			this.Clear();
		}

		int IList.IndexOf(object value)
		{
			return this.IndexOf((T)value);
		}

		void IList.Insert(int index, object value)
		{
			this.Insert(index, (T)value);
		}

		void IList.Remove(object value)
		{
			this.Remove((T)value);
		}

		void IList.RemoveAt(int index)
		{
			this.RemoveAt(index);
		}

		void ICollection.CopyTo(Array array, int index)
		{
			for (int i = index; i < array.Length; i++)
			{
				int sourceIndex = i - index;
				if (sourceIndex >= this.Count)
					return;
				array.SetValue(this[sourceIndex], i);
			}
		}

		public bool IsSynchronized
		{
			get
			{
				return this.ControlCollection.IsSynchronized;
			}
		}

		public object SyncRoot
		{
			get
			{
				return this.ControlCollection.SyncRoot;
			}
		}

		bool IList.IsReadOnly
		{
			get
			{
				return this.IsReadOnly;
			}
		}

		bool IList.IsFixedSize
		{
			get
			{
				return false;
			}
		}

		int ICollection.Count
		{
			get
			{
				return this.Count;
			}
		}

		object ICollection.SyncRoot
		{
			get
			{
				return this.SyncRoot;
			}
		}

		bool ICollection.IsSynchronized
		{
			get
			{
				return this.IsSynchronized;
			}
		}

		object IList.this[int index]
		{
			get
			{
				return this[index];
			}

			set
			{
				this[index] = (T)value;
			}
		}

		#endregion
	}
}